package grupb.upf.imageProcessing;

import java.awt.image.RenderedImage;
import java.util.ArrayList;
import com.sun.media.jai.widget.DisplayJAI;
import grupb.upf.gui.P2Piki;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import javax.swing.SwingUtilities;

public final class DisplayJAIWithAnnotations extends DisplayJAI {
	protected ArrayList annotations;
    private int [] points = new int[4];
    private int nPoints = 0;

	public DisplayJAIWithAnnotations(final RenderedImage image,
            final P2Piki af) {
		super(image); // calls the constructor of DisplayJAI
		annotations = new ArrayList();

		// handler for mouse clicks
		addMouseListener( new MouseAdapter() {
            @Override
			public void mousePressed(MouseEvent e) {
                if (SwingUtilities.isLeftMouseButton(e)) {
                    if (af.isPaintEnabled()) {
                        nPoints = nPoints + 2;
                        CrossAnnotation c1 = new CrossAnnotation(
                                new Point2D.Double(e.getX(), e.getY()),
                                10, 10, 2);
                        c1.setColor(Color.RED);
                        addAnnotation(c1);
                        repaint();
                        if (nPoints <= 4) {
                            points[nPoints - 2] = e.getX();
                            points[nPoints - 1] = e.getY();
                        }
                        if (nPoints == 4) {
                            TagAnnotation c2 = new TagAnnotation(points);
                            c2.setColor(Color.BLACK);
                            addAnnotation(c2);
                            repaint();
                            af.setCoordenates(points);
                        } else if (nPoints > 4) {
                            af.setCoordenates(null);
                            nPoints = 0;
                            points = new int[4];
                            annotations = new ArrayList();
                            repaint();
                        }
                    }
                } else {
                    af.setCoordenates(null);
                    nPoints = 0;
                    points = new int[4];
                    annotations = new ArrayList();
                    repaint();
                }
			}
		});

	}
	// Paints the component and its annotations
    @Override
	public void paint(final Graphics g) {
		super.paint(g);
		Graphics2D g2d = (Graphics2D)g;
		for (int a = 0; a < annotations.size(); a++)
		{
			final DrawableAnnotation element =
                    (DrawableAnnotation) annotations.get(a);
			element.paint(g2d);
		}
	}

	// Adds an annotation to the list of annotations that will be drawn
	public void addAnnotation(final DrawableAnnotation a) {
		annotations.add(a);
	}
    public void removeAnnotations() {
        annotations = new ArrayList();
    }
    public void resetClics() {
        nPoints = 0;
    }
}

